package com.hucsmn.stumansys.service.impl;

import com.hucsmn.stumansys.dao.AdminDao;
import com.hucsmn.stumansys.entity.Admin;
import com.hucsmn.stumansys.service.AdminService;
import com.hucsmn.stumansys.utils.ConvertUtils;

import java.util.List;

/**
 * AdminService 业务逻辑的实现类。
 * @author hucsmn
 */
public class AdminServiceImpl implements AdminService {
    private final AdminDao adminDao = AdminDao.getInstance();

    @Override
    public boolean login(Admin admin) {
        if (!ConvertUtils.validateAdmin(admin)) {
            return false;
        }

        Admin correct = adminDao.getAdminByName(admin.getName());
        if (correct == null || !admin.getCrypt().equalsIgnoreCase(correct.getCrypt())) {
            return false;
        }
        admin.setId(correct.getId());
        return true;
    }

    @Override
    public boolean register(Admin admin) {
        if (!ConvertUtils.validateAdmin(admin)) {
            return false;
        }
        return adminDao.addAdmin(admin);
    }

    @Override
    public boolean unregister(int id) {
        return adminDao.removeAdmin(id);
    }

    @Override
    public boolean modify(Admin admin) {
        if (!ConvertUtils.validateAdmin(admin)) {
            return false;
        }
        return adminDao.setAdmin(admin);
    }

    @Override
    public boolean exists(String name) {
        if (name == null) {
            return false;
        }
        return adminDao.existsAdminName(name);
    }

    @Override
    public List<String> names(String prefix) {
        if (prefix == null || prefix.isEmpty()) {
            return adminDao.getAdminNames();
        }
        return adminDao.getAdminNamesLike(prefix);
    }
}
